package com.pni.games.poker.hands;

import java.util.Arrays;

import com.pni.game.cards.Card;

public class Flush extends PokerHand {
	
	private int[] sortedHandStrength;

	public Flush(Card[] cards) {
		super(cards, HandType.FLUSH);
	}

	@Override
	protected void determineHandValue() throws IllegalArgumentException {
		sortedHandStrength = new int[cards.length];
		
		//only need to do one loop and check to see if all cards have same suit value.
		Card card = cards[0];
		sortedHandStrength[0] = card.getRank().value();
		for(int cardpos = 1; cardpos < cards.length; cardpos++){
			if(cards[cardpos].getSuit() != card.getSuit()){
				throw new IllegalArgumentException(cards.toString() + " not valid combination of cards for flush.");
			}
			sortedHandStrength[cardpos] = cards[cardpos].getRank().value();
			//TODO: check to see if this is wrongfully a straight flush and throw an exception.
		}
		
		Arrays.sort(sortedHandStrength);

	}

	public int[] getSortedHandStrength() {
		return sortedHandStrength;
	}

	@Override
	protected int compareSameHand(PokerHand otherHand) {
		int[] otherFlushValue = ((Flush) otherHand).getSortedHandStrength();
		//start from the highest card and work our way downwards.
		for(int cardpos = cards.length - 1; cardpos >= 0; cardpos--){
			if(sortedHandStrength[cardpos] > otherFlushValue[cardpos]) {
				return 1;
			} else if (sortedHandStrength[cardpos] < otherFlushValue[cardpos]){
				return -1;
			}
		}
		
		return 0;
	}

}
